package response

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

const (
	StatusOK = iota
	StatusFail
)

type JsonResponse struct {
	Code    int         `json:"code"`
	ErrMsg  string      `json:"err_msg"`
	ErrList []ErrMsg    `json:"err_list"`
	Data    interface{} `json:"data"`
}

type ErrMsg struct {
	Field string `json:"field"`
	Name  string `json:"name"`
	Msg   string `json:"msg"`
}

type ErrResponse struct {
	ErrList []ErrMsg `json:"list"`
	ErrMsg  string   `json:"msg"`
}

func SuccessJson(ctx *gin.Context, msg string, data interface{}) {
	if data == nil {
		data = make([]int, 0)
	}

	ctx.JSON(http.StatusOK, JsonResponse{
		Code:    StatusOK,
		ErrMsg:  msg,
		Data:    data,
		ErrList: make([]ErrMsg, 0),
	})
}

func FailJson(ctx *gin.Context, msg string, errList []ErrMsg) {
	if errList == nil {
		errList = make([]ErrMsg, 0)
	}

	ctx.JSON(http.StatusOK, JsonResponse{
		Code:    StatusFail,
		ErrMsg:  msg,
		ErrList: errList,
		Data:    make([]int, 0),
	})
}

func AuthRequired(ctx *gin.Context, msg string) {
	ctx.AbortWithStatusJSON(http.StatusOK, JsonResponse{
		Code:    -1,
		ErrMsg:  msg,
		ErrList: make([]ErrMsg, 0),
		Data:    make([]int, 0),
	})
}
